package com.health.mapp.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.model.AdvertisementList;
import com.health.db.model.AdvertisementListExample;
import com.health.db.model.AdvertisementListTemp;
import com.health.db.service.sm.IAdvertisementListService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryAdvertListReq;
import com.health.mapp.bean.QueryAdvertListResp;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;
/**
 *  查询文章广告
 *
 */
public class QueryAdvertListImpl implements ActionInterface {

	
	private IAdvertisementListService advertisementListService  ;
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
	 
		
		QueryAdvertListReq req = (QueryAdvertListReq) reqBean;
		QueryAdvertListResp _resp = new QueryAdvertListResp();
		
		String  postionId =req.getPostionId(); 
		
		if(StringUtils.isBlank(postionId)&&StringUtils.isBlank(postionId)){
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		
		Integer pageNo=req.getPageNo();
		Integer pageSize=req.getPageNum();
		AdvertisementListExample example = new  AdvertisementListExample();
		example.setPositionid(Long.valueOf(postionId));
		if(null!=pageNo&&null!=pageSize){
			example.setOffset((pageNo.intValue()-1)*pageSize.intValue());
			example.setPageSize(pageSize.intValue());
		}else{
			example.setOffset(0);
			example.setPageSize(20);
		}
		
		List<AdvertisementListTemp> mainAdvice= advertisementListService.selectSimple(example);
		
		List<AdvertisementList> mainAdviceResp= new ArrayList<AdvertisementList>();
		
		for (int i = 0; i < mainAdvice.size(); i++) {
			AdvertisementListTemp temp = mainAdvice.get(i);
			AdvertisementList re = new AdvertisementList();
			re.setAdvertisementDesc(temp.getAdvertisementDesc());
			re.setAdvertisementName(temp.getBannerName());
			re.setAdvertiseTag(temp.getAdvertiseTag());
			re.setLikedAmount(temp.getLikedAmount());
			re.setReadedAmount(temp.getReadedAmount());
			re.setAdvertSort(temp.getBannerOrder());
			re.setAdvertisementPictureUrl(temp.getBannerPic());
			re.setClickLinkUrl(temp.getBannerUrl());
			re.setAdvertisementId(temp.getBannerId().toString());
			re.setShareLinkUrl(temp.getShareLinkUrl());
			re.setCommon(temp.getCommon());
			mainAdviceResp.add(re);
		}
		
		//显示置顶的公告
		if (postionId.equals("3")) {
			AdvertisementListTemp temp = advertisementListService.selectByTop();

			AdvertisementList we = new AdvertisementList();
			if (null!=temp) {
				
				we.setAdvertisementDesc(temp.getAdvertisementDesc());
				we.setAdvertisementName(temp.getBannerName());
				we.setAdvertiseTag(temp.getAdvertiseTag());
				we.setLikedAmount(temp.getLikedAmount());
				we.setReadedAmount(temp.getReadedAmount());
				we.setAdvertSort(temp.getBannerOrder());
				we.setAdvertisementPictureUrl(temp.getBannerPic());
				we.setClickLinkUrl(temp.getBannerUrl());
				we.setAdvertisementId(temp.getBannerId().toString());
			}

			_resp.setTopAdvertisement(we);
		
		}

		_resp.setAdvertList(mainAdviceResp);
		
		return _resp;
	}
	/**
	 * @return the advertisementListService
	 */
	public IAdvertisementListService getAdvertisementListService() {
		return advertisementListService;
	}
	/**
	 * @param advertisementListService the advertisementListService to set
	 */
	public void setAdvertisementListService(
			IAdvertisementListService advertisementListService) {
		this.advertisementListService = advertisementListService;
	}
	
	
	
}
